package com.daiqile.myinterest.fragement;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.daiqile.myinterest.BaseFragment;
import com.daiqile.myinterest.R;
import com.daiqile.myinterest.adapter.ArticleListAdapter;
import com.daiqile.myinterest.customview.ObservableScrollView;
import com.daiqile.myinterest.model.ArticleListDTO;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtools.RxBarTool;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends BaseFragment implements ObservableScrollView.OnObservableScrollViewScrollChanged {


    @BindView(R.id.banner_pic)
    MZBannerView mBanner;
    @BindView(R.id.scrollView)
    ObservableScrollView scrollView;
    @BindView(R.id.layout_assortment_in)
    View assortmentIn;
    @BindView(R.id.layout_assortment_out)
    LinearLayout assortmentOut;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private int bannerHeight;
    private ArticleListAdapter adapter;
    private List<String> bannerList = new ArrayList<>();
    private List<ArticleListDTO> articleList = new ArrayList<>();

    public OneFragment() {
    }

    @Override
    protected int getContentView() {
        return R.layout.fragment_one;
    }

    @Override
    protected void initViews() {
        getBannerHeight();
        initRefresh();
        initBannerList();
        initBanner(bannerList);
        initArticleList();
        scrollView.setOnObservableScrollViewScrollChanged(this);
        setAdapter();
    }

    private void getBannerHeight() {
        mBanner.post(new Runnable() {
            @Override
            public void run() {
                bannerHeight = mBanner.getHeight();
            }
        });
    }

    private void initRefresh() {
        mRefresh.setRefreshHeader(new ClassicsHeader(getActivity()));//设置头部
        mRefresh.setRefreshFooter(new ClassicsFooter(getActivity()));//尾
        //监听
        mRefresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
            }
        });
        mRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);

            }
        });
    }

    private void initBannerList() {
        bannerList.add("http://images6.fanpop.com/image/photos/34200000/IU-iu-34242183-1920-1080.jpg");
        bannerList.add("http://images6.fanpop.com/image/photos/39000000/IU-Desktop-Wallpaper-1920x1080-iu-39096381-1920-1080.jpg");
        bannerList.add("https://wallpapercave.com/wp/WCyGOkj.jpg");
        bannerList.add("https://0.soompi.io/wp-content/uploads/2017/02/20000815/IU.jpg");
        bannerList.add("http://www.asianjunkie.com/wp-content/uploads/2017/04/IUPaletteFace.png");
    }

    public void initBanner(List<String> url) {
        if (url != null && url.size() > 0) {
            mBanner.setDuration(2000);
            mBanner.setPages(url, new MZHolderCreator() {
                @Override
                public MZViewHolder createViewHolder() {
                    return new BannerViewHolder();
                }
            });
            mBanner.start();
        }

    }

    private void initArticleList() {
        for (int i = 0; i < 1; i++){
            ArticleListDTO a = new ArticleListDTO("小姐姐IU",R.drawable.timg7);
            articleList.add(a);
            ArticleListDTO b = new ArticleListDTO("小姐姐IU",R.drawable.iu_a);
            articleList.add(b);
            ArticleListDTO c = new ArticleListDTO("小姐姐IU",R.drawable.iu_b);
            articleList.add(c);
            ArticleListDTO d = new ArticleListDTO("小姐姐IU",R.drawable.iu_c);
            articleList.add(d);
            ArticleListDTO e = new ArticleListDTO("小姐姐IU",R.drawable.iu_d);
            articleList.add(e);
        }
    }

    private void setAdapter() {
        LinearLayoutManager layoutManage = new LinearLayoutManager(getActivity());
        recycleView.setLayoutManager(layoutManage);
        adapter = new ArticleListAdapter(articleList);
        recycleView.setAdapter(adapter);
    }

    @Override
    public void onObservableScrollViewScrollChanged(int l, int t, int oldl, int oldt) {
        if (t >= (bannerHeight - RxBarTool.getStatusBarHeight(getActivity()))) {
            assortmentOut.setVisibility(View.VISIBLE);
        }
        if (t < (bannerHeight - RxBarTool.getStatusBarHeight(getActivity()))) {
            assortmentOut.setVisibility(View.GONE);
        }
    }

    private static class BannerViewHolder implements MZViewHolder<String> {
        private ImageView mImageView;
        @Override
        public View createView(Context context) {
            // 返回页面布局
            View view = LayoutInflater.from(context).inflate(R.layout.banner_item, null);
            mImageView = (ImageView) view.findViewById(R.id.banner_image);
            return view;
        }

        @Override
        public void onBind(Context context, int i, String url) {
            Glide.with(context)
                    .load(url)
                    .error(R.drawable.err)
                    .into(mImageView);
        }
    }

}
